package com.chtw.controller;

import com.chtw.entities.WeChat;
import com.chtw.utils.aec.AesException;
import com.chtw.utils.aec.SHA1;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * @author chtw
 * @date 2020-06-19 07:14
 */
@RestController
@Slf4j
public class WeChatController {

    @Value("${token}")
    private String token;

    @RequestMapping(value = "/wx", method = RequestMethod.GET)
    public String fromWeChatServer(WeChat weChat) {
        String signature = weChat.getSignature();
        log.info("weChat:" +weChat.toString());
        long timestamp = weChat.getTimestamp();
        int nonce = weChat.getNonce();
        String echostr = weChat.getEchostr();
        String encrypt = weChat.getEncrypt();

        String pass = null;
        try {
            pass = SHA1.getSign(token, String.valueOf(timestamp), String.valueOf(nonce));
        } catch (AesException e) {
            e.printStackTrace();
        }
        log.info("pass：" + pass + "signature：" + signature);
        if(signature.equals(pass)) {
            return echostr;
        }
        return null;
    }

    @RequestMapping(value = "/getToken", method = RequestMethod.GET)
    public String s(){
        return token;
    }

}
